package com.jetgege.smartbox.BoxSetting;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.jetgege.smartbox.Model.UserModel;
import com.jetgege.smartbox.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JJT-ssd on 2017/5/8.
 */

public class UserAdapter extends BaseAdapter{
    List<UserModel> users =null;
    List<CheckBox> checkBoxes = new ArrayList<>();
    List<ImageView> imageViews = new ArrayList<>();
    public UserAdapter( List<UserModel> users)
    {
        this.users=users;
    }
    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view =null;
        ViewHolder viewHolder=null;
        if (convertView==null)
        {
            viewHolder=new ViewHolder();
            view= LayoutInflater.from(parent.getContext()).inflate(R.layout.box_setting_list_item,parent,false);
            viewHolder.tv_ItemName=(TextView) view.findViewById(R.id.tv_ItemName);
            viewHolder.tv_ItemTip=(TextView) view.findViewById(R.id.tv_ItemTip);
            viewHolder.cb_UserEdit=(CheckBox) view.findViewById(R.id.cb_UserEdit);
            viewHolder.iv_UserGo=(ImageView) view.findViewById(R.id.iv_UserGo);
            view.setTag(viewHolder);
        }else
        {
            view=convertView;
            viewHolder=(ViewHolder)convertView.getTag();
        }

        if (checkBoxes.size()>position){
            checkBoxes.clear();
            imageViews.clear();
        }
        viewHolder.cb_UserEdit.setChecked(false);
        checkBoxes.add(viewHolder.cb_UserEdit);
        imageViews.add(viewHolder.iv_UserGo);
        viewHolder.tv_ItemName.setText(users.get(position).getUserName());
        switch (users.get(position).getUserType()){
            case 1:
                viewHolder.tv_ItemTip.setText("管理员");
                viewHolder.tv_ItemTip.setBackgroundResource(R.drawable.btn1_pressed);
                break;
            case 2:
                viewHolder.tv_ItemTip.setText("用户");
                viewHolder.tv_ItemTip.setBackgroundResource(R.drawable.btn2_normal);
                break;
            default:break;
        }
        return view;
    }

    private class ViewHolder
    {
        TextView tv_ItemName;
        TextView tv_ItemTip;
        CheckBox cb_UserEdit;
        ImageView iv_UserGo;
    }
}
